package studio.banner.reference.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import studio.banner.reference.entity.UserInfo;
import studio.banner.reference.service.UserInfoService;
import studio.banner.reference.util.ResultBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("/info")
@Controller
public class UserinfoController {
	@Autowired
	private UserInfoService userInfoService;

	/*
	 * 查询这个账号是否已经被注册了，如果已经被注册了，返回错误信息。 如果没有被注册，则写入数据库。
	 */
	@RequestMapping("/userinfo")
	public String userinfopage() {
		return "/user/userinfo";
	}

	/*
	 * 注册信息
	 */
	@RequestMapping(value = "/register")
	public String register(HttpServletRequest request, HttpServletResponse response) {
		String studentid = request.getParameter("studentid");
		String password = request.getParameter("password");
		String scoolname = request.getParameter("scoolname");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String phonenumber = request.getParameter("phonenumber");
		String address = request.getParameter("address");
		UserInfo ul = new UserInfo();
		ul.setAddress(address);
		ul.setName(name);
		ul.setPhonenumber(phonenumber);
		ul.setRole("user");
		ul.setScoolname(scoolname);
		ul.setSex(sex);
		ul.setStudentid(studentid);
		Integer result = userInfoService.findByUserName(studentid);
		if (result == 0) {
			new ResultBean<>(userInfoService.insertUserandLogin(ul, password));
			return "redirect:/login/";
		} else {
		new ResultBean<>(0);
		}
		return "/info/userinfo";
	}

	/*
	 * 更新用户信息
	 */
	@RequestMapping("/update")
	public ResultBean<Integer> update(@RequestBody UserInfo user) {
		userInfoService.update(user);
		return new ResultBean<>(userInfoService.update(user));
	}

}
